//
//  WWNavigationBarView.m
//  xiangyundong
//
//  Created by douniwan on 2019/8/27.
//  Copyright © 2019 douniwan. All rights reserved.
//

#import "WWNavigationBarView.h"


@interface  WWNavigationBarView ()

@property (nonatomic, strong)   UILabel *titleLabel;
@property (nonatomic, strong)   UIButton *returnButton;
@property (nonatomic, strong)   UIButton *rightButton;

@property (nonatomic, strong)   UIView *containerView;

@end


@implementation WWNavigationBarView

#pragma mark - ♻️life cycle
-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
        self.rightButton.hidden = YES;  // 默认设置为不可见
    }
    return self;
}

#pragma mark - 🔄overwrite

#pragma mark - 🚪public

- (void)setupWithTitle:(NSString *)title target:(id)target action:(SEL)action
{
    self.titleLabel.text = title;
    [self.returnButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
}

- (void)setupWithTitle:(NSString *)title barBackgroundColor:(UIColor *)color target:(id)target action:(SEL)action
{
    [self setupWithTitle:title target:target action:action];
    
    self.backgroundColor = color;
    self.containerView.backgroundColor = color;
}

- (void)setRightButtonWithTitle:(NSString *)title target:(id)target action:(SEL)action
{
    self.rightButton.hidden = NO;   // 默认隐藏，需要设置时设为显示
    [self.rightButton setTitle:title forState:UIControlStateNormal];
    [self.rightButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
}

- (void)setRightButtonWithImage:(UIImage *)image target:(id)target action:(SEL)action
{
    self.rightButton.hidden = NO;   // 默认隐藏，需要设置时设为显示
    [self.rightButton setImage:image forState:UIControlStateNormal];
    [self.rightButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - 🍐delegate

#pragma mark - ☎️notification

#pragma mark - 🎬event response

#pragma mark - 🔒private

#pragma mark - 🌲setupUI
-(void)setupUI{
    self.containerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    self.containerView.backgroundColor = NTMainColor;
    
    [self addSubview:self.containerView];
    [self addSubview:self.titleLabel];
    [self addSubview:self.returnButton];
    [self addSubview:self.rightButton];
    
    [self.returnButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(12*ScaleX);
        make.width.mas_equalTo(56*ScaleX);
        make.height.mas_equalTo(47*ScaleX);
        make.bottom.equalTo(self);
    }];
    
    [self.rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-12*ScaleX);
        make.width.mas_equalTo(56*ScaleX);
        make.height.mas_equalTo(47*ScaleX);
        make.bottom.equalTo(self);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.returnButton.mas_right);
        make.right.equalTo(self.rightButton.mas_left);
        make.height.mas_equalTo(47*ScaleX);
        make.bottom.equalTo(self);
    }];
}

#pragma mark - ☸getter and setter

-(UILabel *)titleLabel{
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.font = [UIFont boldSystemFontOfSize:18*ScaleX];
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.backgroundColor = [UIColor clearColor];
    }
    return _titleLabel;
}


-(UIButton *)returnButton{
    if (_returnButton == nil) {
        _returnButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_returnButton setImage:[UIImage imageNamed:@"coin_back_white"] forState:UIControlStateNormal];
        _returnButton.titleLabel.font = [UIFont systemFontOfSize:18*ScaleX];
        _returnButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    }
    return _returnButton;
}

-(UIButton *)rightButton{
    if (_rightButton == nil) {
        _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightButton.titleLabel.font = [UIFont systemFontOfSize:18*ScaleX];
    }
    return _rightButton;
}




@end
